/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pgentity;

import pgentity.quest.QuestLogger;
import pgentity.services.QuestServices;
import share.PGException;

/**
 *
 * @author KieuAnh
 */
public class EntityContext implements PGEntity
{
    private final User user;
    private Cote cote;
    private BoxEgg boxEgg;
    private Penguindex penguindex;
    private Inventory inv;
    private QuestLogger logger;

    private EntityContext(User user) {
        this.user = user;
    }
    
    public static EntityContext getContext(String uid)
            throws PGException
    {
        User user = User.getUser(uid);
        return new EntityContext(user);
    }
    
    public static EntityContext getContext(String uid, String coteID)
            throws PGException
    {
        User user = User.getUser(uid);
        EntityContext context = new EntityContext(user);
        Cote cote = Cote.getCote(uid, coteID);
        context.cote = cote;
        
        return context;
    }
    
    public static EntityContext getContext(User user)
    {
        return new EntityContext(user);
    }
    
    public static EntityContext getContext(User user, Cote cote)
    {
        EntityContext context = new EntityContext(user);
        context.cote = cote;
        
        return context;
    }
    
    public static EntityContext getContext(User user, Cote cote, BoxEgg boxEgg,
            Penguindex pdex)
    {
        EntityContext context = new EntityContext(user);
        context.cote = cote;
        context.boxEgg = boxEgg;
        context.penguindex = pdex;
        
        return context;
    }

    public User getUser() {
        return user;
    }

    public Cote getCote() {
        if (cote == null)
        {
            final String coteID = CoteList.getCotes(user.getUid()).at(0);
            cote = Cote.getCote(user.getUid(), coteID);
        }
        return cote;
    }

    public BoxEgg getBoxEgg() {
        if (boxEgg == null)
        {
            String coteID = getCoteID();
            boxEgg = BoxEgg.getBoxEgg(user.getUid(), coteID);
        }
        return boxEgg;
    }

    public Penguindex getPenguindex() {
        if (penguindex == null)
        {
            penguindex = Penguindex.getPenguindex(user.getUid());
        }
        return penguindex;
    }
    
    public Inventory getInv()
    {
        if (inv == null)
        {
            inv = Inventory.getInventory(user.getUid());
        }
        
        return inv;
    }

    @Override
    public void updateFromDB()
    {
        user.updateFromDB();
        
        if (cote != null)
        {
            cote.updateFromDB();
        }
        
        if (boxEgg != null)
        {
            boxEgg.updateFromDB();
        }
    }

    @Override
    public void saveToDB() {
        user.saveToDB();
        
        if (cote != null)
        {
            cote.saveToDB();
        }
        if (boxEgg != null)
        {
            boxEgg.saveToDB();
        }
    }
    
    public String getUid()
    {
        return user.getUid();
    }
    
    public String getCoteID()
    {
        return (cote != null)?cote.getCoteID():CoteList.getCotes(user.getUid()).at(0);
    }
}